package sg.edu.nus.iss.vmcs.customer;

import java.awt.*;

import sg.edu.nus.iss.vmcs.store.DrinksBrand;
import sg.edu.nus.iss.vmcs.store.Store;
import sg.edu.nus.iss.vmcs.store.StoreItem;

/**
* This interface object is part of the Customer Panel. It is used by the Customer to select a drink. 
* It inherits from the Java AWT Panel Class.
* @version 3.0 29/11/2011
* @author  Team 7S - SE19 
*/

public class DrinkSelectionBox extends Panel {

	private int[] drinkId;
	private TransactionController tranCtrl;
	private DrinkSelectionItem[] drinkItems;

/**
 * This constructor creates an instance of the DrinkSelectionBox. It receives the
 * reference for Container and Transaction Controller object as parameters.
 * @param composite Container of components
 * @param tCtrl Transaction Controller
 * @see TransactionController
 */
	public DrinkSelectionBox(Container composite, TransactionController tCtrl) {

		tranCtrl = tCtrl;
		this.initDrinkSelection();
		this.setLayout(new GridLayout(0,1));
	}

/**
 * This operation initializes the Drink Selection Box.
 */	
	public void initDrinkSelection () {				
		StoreItem[] items = tranCtrl.getStoreController().getStoreItems(Store.DRINK);
		drinkItems = new DrinkSelectionItem[items.length];
		drinkId    = new int[items.length];
		
		Panel tp = new Panel(new GridLayout(drinkId.length,1));
		for (int i = 0; i < items.length; i++) {
			DrinksBrand ob = (DrinksBrand) items[i].getContent();
			drinkItems[i] = new DrinkSelectionItem();
			drinkItems[i].setName(ob.getName());
			drinkItems[i].setPrice(ob.getPrice());
			drinkItems[i].setItemState(items[i].getQuantity() == 0 ? false : true);
			drinkId[i] = i;
			drinkItems[i].addListener(new DrinkSelectionListener(tranCtrl));
			tp.add(drinkItems[i]);
		}
		this.add(tp);
	}

/**
 * This operation sets the action command to the drinks items.
 */	
	public void setAction() {
		int i;
		for (i = 0; i < drinkId.length; i++) {
			drinkItems[i].setActionCommand(String.valueOf(i));
		}
	}
	
/**
 * Updates the stock status, name and price of a drink brands based on the 
 * values received. 
 * @param brandIndex Index of Drinks Brand
 * @param quatity Quantity of Drinks Brand
 * @param price Price of Drinks Brand
 * @param name Name of Drinks Brand
 */	
	public void update(int brandIndex, int quatity, int price,String name){
		drinkItems[brandIndex].setName(name);
		drinkItems[brandIndex].setPrice(price);
		drinkItems[brandIndex].setItemState(quatity == 0 ? false : true);
	}

/**
 * This operation activates or deactivates the drink selection buttons on the Customer Panel. 
 * @param brandIndex Index of the Drinks Item object
 * @param active boolean state for setting true or false the drinks item object
 */	
	public void setState(int brandIndex,boolean active){
		drinkItems[brandIndex].setItemActive(active);
	}

/**
 * This operation sets the drink item active or deactive
 * @param active boolean active / deactive
 */	
	public void setActive(boolean active){
		for(int i : drinkId){
			drinkItems[i].setItemActive(active);
		}
	}

/**
 * This operation activates or deactivates the drink selection buttons on the Customer Panel.
 * This operation also displays OUT OF STOCK messages where appropriate. 
 * @param brandIndex Index of Drinks Brand object
 * @param active boolean state for setting drinks brand object as active or deactive
 */	
	public void setItemState(int brandIndex,boolean active){
		drinkItems[brandIndex].setItemState(active);
	}
}
